<?php

	class Campaign_controller extends CI_Controller {
            
		function __construct(){
                    parent::__construct();
                    $this->load->library('session');
                    $this->load->helper('url');
                    $this->load->helper('utils');
		}

		function index(){
                    
                    $cfer = new Cfer(array(
                    'Dashboard' => 'dashboard',
                    'Your Campaigns' => base_url('campaign')));
                    
                    
                    $this->data['page_title'] = 'Your Campaigns';
                    
                    $id_user = 1;
                    $campaign = new Campaign();
                    $campaign->addWhere('id_user = '.$id_user);
                    $campaign->addWhere('is_deleted = 0');
                    $campaign->find();
                    
                    $this->data['campaign'] = $campaign;
                    
                    $this->load->view('campaign', $this->data);
		}
                
                function form($id_campaign = null) {
                    
                    //$facebook = Network::getNetwordById(1);                    
                    //$facebook = new Facebook($facebook->app_id, $facebook->secret);
                    //$user = $facebook->getUser();
                    
                    $cfer = new Cfer(array(
                    'Your Campaigns' => 'campaign',
                    'Create a new Campaign' => base_url('campaign/form')));
                    
                    $act = $this->input->post('act');
                    $url = $this->input->post('url');
                    $target = $this->input->post('target');
                    $countries = $this->input->post('country');
                    $start_date = $this->input->post('start_date');
                    
                    if (is_array($countries) && count($countries) > 0)
                        $countries = implode(',', $countries);
                    else
                        $countries = '';
                    
                    $campaign = new Campaign();
                    
                    
                    if ($id_campaign) {
                        
                        $campaign->get($id_campaign);
                        
                        if (!$campaign->id)
                            redirect(base_url('campaign'));
                        
                    }
                    
                    if ($act == ACT_SUBMIT) {
                        
                        $obj = Facebook::getFanPage($url);
                        
                        if ($obj) {
                            if ($fan_page = FacebookFanPage::updateData($obj)) {
                                $campaign->name = $fan_page->name;
                                $campaign->image = $fan_page->picture;
                                $campaign->url = $fan_page->link;
                                $campaign->description = $fan_page->description;
                            }
                        }
                        
                        if ($start_date && trim($start_date) != '')
                            $campaign->start_date = $start_date;
                            
                        $campaign->country = $countries;

                        if (MessageHandler::countError() == 0) {
                            if (!$id_campaign) {
                                $campaign->status = CREATED;
                                $campaign->click_target = $target;
                                $campaign->created_date = date('Y-m-d h:i:s');
                                $campaign->id_user = 1;
                                $campaign->id_network = 1;

                                if ($campaign->validateInput()) {
                                    if ($campaign->insert())
                                        redirect(base_url('campaign'));
                                }

                            }
                            else {
                                if ($campaign->status == CREATED)
                                    $campaign->click_target = $target;

                                if ($campaign->validateInput())
                                    if ($campaign->update())
                                        redirect(base_url('campaign'));
                            }
                        }
                    }
                    
                    if (isValidDBDate($campaign->start_date)) {
                        $campaign->start_date = date_sql_to_date($campaign->start_date);
                    }
                    
                    $like_price = Setting::getInstance('LIKE_PRICE');
                    $prices_list = OrderCoeff::getCalculatedPrices($like_price->val_1, 0);
                    
                    $country = new Country();
                    $country->addJoin(new Region());
                    $country->addSelect();
                    $country->addSelect('country.*, region.name region_name');
                    $country->addSelect("FIND_IN_SET(country.code, '$campaign->country') selected");
                    $country->addWhere('country.is_deleted = 0');
                    $country->orderBy('region.ordered, country.name');
                    $country->find();
                    
                    $this->data['campaign'] = $campaign;
                    $this->data['country'] = $country;
                    $this->data['prices_list'] = $prices_list;
                    $this->data['back'] = base_url('campaign');
                    
                    $this->load->view('campaign_form', $this->data);
                }

		// You can place some more methods code here

	}
